//
//  main.c
//  http://code.google.com/p/dpi
//
//  Created by Nicholas Shanks on 17/11/2006.
//  Copyright Nicholas Shanks 2006. All rights reserved.
//	Released under the MIT license. Do with this as you please.
//

#include <ApplicationServices/ApplicationServices.h>

int main(int argc, char * argv[])
{
	int i;
	CGDisplayCount displayCount;
	CGDisplayErr error = CGGetActiveDisplayList(0, NULL, &displayCount);
	if(error) return paramErr;
	CGDirectDisplayID activeDisplays[displayCount];
	error = CGGetActiveDisplayList(displayCount, activeDisplays, &displayCount);
	if(error) return paramErr;
	for(i = 0; i < displayCount; i++) {
//		CGDirectDisplayID display = CGMainDisplayID();
		CGDirectDisplayID display = activeDisplays[i];
		CGSize size = CGDisplayScreenSize(display);
		size_t width = CGDisplayPixelsWide(display);
		size_t height = CGDisplayPixelsHigh(display);
		uint32_t unit = CGDisplayUnitNumber(display);
		uint32_t vendor_id = CGDisplayVendorNumber(display);
		uint32_t model_id = CGDisplayModelNumber(display);
		uint32_t serial = CGDisplaySerialNumber(display);
		boolean_t builtin = CGDisplayIsBuiltin(display);
		boolean_t main = CGDisplayIsMain(display);
		const char * vendor_name = NULL;
		switch(vendor_id) {
			case 1552: {
				// I measured these by hand
				vendor_name = "Apple";
				switch (model_id) {
					case 37398:		// 22" Acrylic ADC Cinema Display
						size.width = 471.0f;
						size.height = 301.0f;
						break;
					
					case 37405:		// 20" Aluminium DVI Cinema Display
						size.width = 434.0f;
						size.height = 272.0f;
						break;
					
					case 40040:		// 15" MacBookPro3,1
						size.width = 334.0f;
						size.height = 208.7f;
						break;
					
					case 40117:		// 27" iMac11,1
						// well I would measure this if I could find my tape measure
						break;
				}
			}
		}
		
		char * vendor = NULL;
		if(vendor_name != NULL)
		     asprintf(&vendor, "%u [%s]", vendor_id, vendor_name);
		else asprintf(&vendor, "%u", vendor_id);
		printf("Display %u (vendor=%s; model=%u; serial=%u%s%s)\n  Width: %.1f mm\n  Height: %.1f mm\n  H Res: %.3f px/mm (%.1f DPI)\n  V Res: %.3f px/mm (%.1f DPI)\n", unit, vendor, model_id, serial, builtin ? "; built-in":"", main ? "; menu-bar":"", size.width, size.height, width/size.width, 25.4 * width/size.width, height/size.height, 25.4 * height/size.height);
	}
	return 0;
}
